This section starts with an Extension Suffix Overview, then provides information on How to Check for OpenGL Extension Availability and an Example Program: Checking for Extension Availability.
Function names and tokens for OpenGL extensions have EXT or a vendor-specific acronym as a suffix, for example glVertexPointerEXT() or glColorTableSGI(). The names of the extensions themselves (the extension strings) use prefixes, for example, SGI_color_table. Below is a detailed list of all suffixes and prefixes:
All supported extensions have a corresponding definition in gl.h and a token in the extensions string returned by glGetString(). For example, if the vertex array extension (EXT_vertex_array) is supported, it is defined in gl.h as follows:
#define SGI_COMPILED_VERTEX_ARRAY 1
GL_SGI_compiled_vertex_array appears in the extensions string returned by glGetString(). Use the definitions in gl.h at compile time to determine if procedure calls corresponding to an extension exist in the library.
Applications should do compile-time checking--for example, making sure GL_SGI_compiled_vertex_array is defined; and run-time checking--for example, making sure GL_SGI_compiled_vertex_array is in the extension string returned by glGetString().
Note that availability depends not only on the operating system but also on the particular hardware you are using: even though the OpenGL library supports SGI_compiled_vertex_array, the underlying hardware may not.
Don't call glGetString() until a rendering context has been bound. When rendering under the X Window System, it's critical to know what kind of machine the display is on because different X displays can have different capabilities. While this may not be so critical under Windows, it's important not to forget it for portability reasons.
The function wglGetProcAddress() returns the address of an OpenGL extension function to use with the current OpenGL rendering context. For example, to use the extension command glVertexPointerEXT(), an application can obtain the address of the function to call by calling wglGetProcAddress() by following these steps:
typedef void (APIENRY *PFNGLVERTEXPOINTEREXTPROC)
(GLint size, GLenum type, glsizei stride,
GLsizei count, const GLvoid *pointer);
PFNGLVERTEXPOINTEREXTPROC glVertexPointerEXT;
glVertexPointerEXT =
(PFNGLVERTEXPOINTEREXTPROC)
wglGetProcAddress ("glVertexPointerEXT");
(*glVertexPointerEXT)(3, GL_FLOAT, 0, 0, data);
In Example 3-1, the function QueryExtension() checks whether an extension is available.
Example 3-1 : Checking for Extensions
main(int argc, char* argv[]) { ... if (!QueryExtension("GL_EXT_paletted_texture")) { fprintf(stderr, "paletted texture extension not supported.\n"); exit(1); } ... } static GLboolean QueryExtension(char *extName) { /* ** Search for extName in the extensions string. Use of strstr() ** is not sufficient because extension names can be prefixes of ** other extension names. Could use strtok() but the constant ** string returned by glGetString might be in read-only memory. */ char *p; char *end; int extNameLen; extNameLen = strlen(extName); p = (char *)glGetString(GL_EXTENSIONS); if (NULL == p) { return GL_FALSE; } end = p + strlen(p); while (p < end) { int n = strcspn(p, " "); if ((extNameLen == n) && (strncmp(extName, p, n) == 0)) { return GL_TRUE; } p += (n + 1); } return GL_FALSE; }